home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / chapter13 / ClipRect.java < prev    next >
Text File  |  1995-12-31  |  2KB  |  65 lines

  1.  
  2. import java.awt.*;
  3.  
  4. public class ClipRect extends DoubleBuff implements Runnable {
  5.  
  6.    boolean drewall = false;  // whole screen drawn yet?
  7.  
  8.  
  9.  
  10.    /* The paint method */
  11.    public void paint(Graphics g) {
  12.  
  13.       if (!drewall) g.clipRect(0,0,XRES,YRES);
  14.     else
  15.      if (y > oldy) g.clipRect(x,oldy,w+1,y+h);
  16.       else
  17.        g.clipRect(x,y,w+1,oldy+h);
  18.  
  19.  
  20.       dbuffer.setColor(Color.lightGray); // background color
  21.       dbuffer.fillRect(x,oldy,w+1,h+1);  // cover up last box drawn
  22.       oldy=y;               // remember y for the next coverup
  23.  
  24.       /* draw background pattern */
  25.       for(int gc=0;gc<XRES;gc+=gw) { 
  26.         dbuffer.setColor(Color.gray);        // color of gridlines
  27.         dbuffer.drawLine(gc,0,gc,YRES);      // draw vertical lines
  28.         dbuffer.drawLine(0,gc,XRES,gc);      // draw horizontal lines
  29.         dbuffer.setColor(Color.yellow);      // set color to yellow
  30.         dbuffer.drawLine(gc,0,0,gc);         // draw diags
  31.         dbuffer.setColor(Color.blue);        // set color to blue
  32.         dbuffer.drawLine(XRES-gc,0,XRES,gc); // draw diags
  33.         }
  34.  
  35.       dbuffer.setColor(Color.lightGray); // background color
  36.       dbuffer.fillRect(0,0,100,20);      // clear box for title
  37.       dbuffer.setColor(Color.red);       // set color to red
  38.       dbuffer.drawString("SingleBuffer",0,10);
  39.       dbuffer.fillRect(x,y,w,h);          // draw red square
  40.       dbuffer.setColor(Color.blue); 
  41.  
  42.       int l=0;
  43.       for (int lh=h;lh>0;l+=4,lh-=8)   
  44.           dbuffer.drawOval(x+l,y+l,lh,lh);
  45.  
  46.     /* Notice that the only call to the g object    */
  47.     /* is on the following line. All other graphics */
  48.     /* operations happen to the buffer         */
  49.  
  50.     /* Transfer (blit) the offscreen to the screen  */
  51.       g.drawImage(offscreen,0,0,this);
  52.       drewall = true;
  53.  
  54.       }
  55.  
  56.     public void start() {
  57.       drewall = false;
  58.       if (MyThread == null) {
  59.         MyThread = new Thread(this);
  60.         MyThread.start();
  61.      }
  62.      }
  63.  
  64.     }
  65.